package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.GoodImage;
import com.wuming.house.model.Information;
import com.wuming.house.service.GoodImageService;
import com.wuming.house.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author xuhuai
 * @createTime 2019/8/27 10:35
 * @Description:
 */
@Controller
@RequestMapping("/web/information")
public class InformationController extends BaseController {

    @Autowired
    private InformationService informationService;
    @Autowired
    private GoodImageService goodImageService;

    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public CommonResult list(@RequestParam(required = false)String title, @RequestParam(required = false)String source,
                             @RequestParam(defaultValue = "1")int pageIndex, @RequestParam(defaultValue = "10")int pageSize){
        logger.info("准备查询资讯头条");
        try {
            List<Information> infos = informationService.getInfosByPage(title,source,pageIndex,pageSize);
            int total = informationService.getTotal(title,source);
            return CommonResult.createSuccessResult(infos,total,"查询资讯头条成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("发生业务异常,异常信息:{}",e.getMessage());
        }
        return CommonResult.createFailureResult("查询资讯头条失败");
    }

    @RequestMapping("/toManageInformation")
    public String toManageInformation(){
        return "/tools/manageInformation";
    }

    @RequestMapping("/toEditInformation/{type}/{id}")
    public String toEditInformation(@PathVariable Integer type, @PathVariable Integer id, Model model){
        if (type==2){
            if (null!=id){
                model.addAttribute("information",informationService.getInformationById(id));
            }
        }
        return "/tools/editInformation";
    }

    @ResponseBody
    @RequestMapping(value = "/edit")
    public JsonResult edit(Information information){
        if (null == information.getId()) {
            information.setCreateDatetime(new Date());
            informationService.saveInformation(information);
            return new JsonResult(null,"添加资讯成功");
        } else {
            informationService.modifyInformation(information);
            return new JsonResult(null,"更新资讯成功");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public JsonResult delete(@PathVariable Integer id){
        logger.info("准备删除id:{}的资讯",id);
        try {
            informationService.delete(id);
            return new JsonResult(null,"删除成功",true);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("发生业务异常,异常信息:{}",e.getMessage());
            return new JsonResult(null,"删除失败",false);
        }
    }

    @ResponseBody
    @RequestMapping("/uploadImg")
    public JsonResult uploadImg(MultipartFile file, HttpServletRequest request){
        try {
            String path = this.uploadImage(file,"information",1);
            logger.info("文件成功上传到指定目录下,文件目录:{},",path);
            return new JsonResult(path,"上传成功",true);
        }catch (IOException ie){
            ie.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult("","上传失败",false);
    }

    @ResponseBody
    @RequestMapping("/get/{infoId}")
    public JsonResult get(@PathVariable Integer infoId){
        try {
            Information information = informationService.getInformationById(infoId);
            return new JsonResult(information.getUrl(),"",true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(null,"",false);
    }
}
